package com.hsxc.cdz.monitor.FSM;

import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hsxc.cdz.base.bean.Reserv;
import com.hsxc.cdz.base.util.TimeFormatter;
import com.hsxc.cdz.mgmt.GlobalManager;
import com.hsxc.cdz.monitor.executor.ServerExecutorNew;
import com.hsxc.cdz.monitor.executor.ServerExecutor;

public class PileMemStateIdle extends PileMemState {

	private Logger logger = LoggerFactory.getLogger(PileMemStateIdle.class);

	// 构造函数
	public PileMemStateIdle(CachePile pile) {
		this.status = Status.PILE_STATUS_IDLE;
		this.pile = pile;
		// this.pile.state = new PileMemStateIdle(/*this.pile*/);
	}

	// event actions
	// ===========================================================================
	// public static int PILE_STATUS_LOCKED=0;//表示已经被占用
	// public static int PILE_STATUS_FAULT=1; //表示电桩故障
	// public static int PILE_STATUS_UNINSERT=2;//表示未插入充电接口
	// public static int PILE_STATUS_INSERTED=3;//表示已经插入充电接口
	/**
	 * return 1--表示故障成功 0--表示故障失败
	 */
	int fault() {
		// handleFault();

		// 状态转移
		if (this.status != Status.PILE_STATUS_FAULT) {
			this.status = Status.PILE_STATUS_FAULT;
			this.pile.state = new PileMemStateFault(this.pile);
			// 更新DB
			updatePileStatus(pile);
			//更新缓存
			GlobalManager.pileRegister.delPileManually(this.pile.getAddress());
			return 1;
		}

		return 0;
	}

	int recovery() {
		return 1;
	}

	// public static int PILE_STATUS_LOCKED=0;//表示已经被占用
	// public static int PILE_STATUS_FAULT=1; //表示电桩故障
	// public static int PILE_STATUS_UNINSERT=2;//表示未插入充电接口
	// public static int PILE_STATUS_INSERTED=3;//表示已经插入充电接口
	@Override
	int lock() {
		// 实现判断充电桩是否可用
		//if (this.pile.isInserted()) {
		if (true) {
			this.pile.state = new PileMemStateLocked(this.pile);
			// 更新数据库
			updatePileStatus(pile);
			return CachePile.PILE_STATUS_INSERTED;
		} else {
			return CachePile.PILE_STATUS_UNINSERT;
		}
	}

	@Override
	int unlock() {
		return 1;
	}

	@Override
	int heartBeatLost() {
		return 1;
	}

	@Override
	public int logout() {
		this.pile.state = new PileMemStateIdle(this.pile);
		updatePileStatus(this.pile);
		GlobalManager.pileManager.delPile(this.pile.getAddress());
		return 0;
	}

	@Override
	public int reboot() {
		ServerExecutor executor = new ServerExecutor();
		int i = executor.restart(this.pile.getAddress(), this.pile.getProtocolVersion(), this.pile.getPileType());
		if (i == 0) {
			return 0;
		}
		return 1;
	}

	@Override
	public int chargeStart_jiaoliu(int chargePort, int startWay, int ctrlWay, int stopWay, double limitData,
			String startTime, String cardNum, String pileAddress, String operatorNO, String protocol, int pileType) {
		return 1;
	}

	@Override
	public int chargeStart_zhiliu(int chargePort, int startWay, int ctrlWay, int stopWay, double limitData,
			String startTime, String cardNum, String pileAddress, String operatorNO, int chargeDataInterval,
			int batteryDataInteval, String protocol, int pileType) {
		return 1;
	}

	@Override
	public boolean confirmStart(int chargePort, String pileNo, String userNo, String protocol, int pileType) {
		return false;
	}

	@Override
	public int chargeStop(int chargePort, int control, String userNO, String protocol, int pileType) {
		// 电桩主动停止充电后，桩变为空闲态，需要再发确认停止指令
		if (this.pile.getProtocolVersion().equals("121") || this.pile.getPileType() == 0) {
			return ServerExecutor.stopCharge(0, 0, 0, chargePort, userNO, this.pile.getAddress(),
					this.pile.getOperator(), this.pile.getProtocolVersion(), this.pile.getPileType());
		} else {
			return ServerExecutorNew.stopCharge(0, 0, 0, chargePort, userNO, this.pile.getAddress(),
					this.pile.getOperator(), protocol, pileType);
		}

	}

	@Override
	public boolean confirmStop(int chargePort, String pileNo, String userNo, String protocol, int pileType) {
		// 电桩主动停止充电后，桩变为空闲态，需要再发确认停止指令
		return true;
	}

	@Override
	int reserve(int chargePort, String cardNo, int duration, String pileAddress, String operatorNo, String protocol,
			int pileType) {
		if (this.pile.getProtocolVersion().equals("121") || this.pile.getPileType() == 0) {
			return ServerExecutor.reserve(chargePort, cardNo, pileAddress, operatorNo, duration, protocol, pileType);
		} else {
			return ServerExecutorNew.reserve(chargePort, cardNo, this.pile.getAddress(), this.pile.getOperator(),
					duration, protocol, pileType);
		}
	}

	@Override
	int confirmReserv_jiaoliu(int chargePort, String cardNo, String pileAddress, String operator, String protocol,
			int pileType) {
		this.pile.state = new PileMemStateReserved(this.pile);
		updatePileStatus(this.pile);
		//更新缓存的桩
		GlobalManager.pileManager.newPile(this.pile);
		return 0;
	}

	@Override
	int confirmReserv_zhiliu(int chargePort, String cardNo, String resStartTime, String pileAddress, String operator,
			String protocol, int pileType) {
		Reserv reserv = GlobalManager.cacheReservManager.queryReserv(cardNo,this.pile.getAddress());
		if (reserv == null) {
			logger.info("用户号{}" + cardNo + "没有预约记录");
			logger.info("CHARGEPORT==" + chargePort + "RESSTARTTIME==" + resStartTime + "PILEADDRESS==" + pileAddress);
			return 1;
		}
		reserv.setReservStartTime(resStartTime);
		Calendar calendar = TimeFormatter.dateFromString(resStartTime);
		calendar.add(Calendar.SECOND, reserv.getDuration());
		reserv.setReservStopTime(TimeFormatter.strFromCalendar(calendar));
		int result = GlobalManager.reservManager.updateReserv(reserv.getResNo(), resStartTime,
				reserv.getReservStopTime(), false);
		//更改电桩状态
		this.pile.state = new PileMemStateReserved(this.pile);
		updatePileStatus(this.pile);
		GlobalManager.pileManager.newPile(this.pile);
		if (result == 1) {
			return 0;
		} else {
			return 1;
		}
	}

	@Override
	int reservationCancel(int chargePort, String cardNum, String pileadress, String operatorNo, String protocol,
			int pileType) {
		return 1;
	}

	@Override
	int confirmReservCancelled(int chargePort, String cardNo, String pileAddress, String operatorNo, String protocol,
			int pileType) {
		// TODO Auto-generated method stub
		return 1;
	}

}
